package cn.android.basic.components.span

import android.graphics.Typeface
import android.text.SpannableStringBuilder
import android.text.style.ClickableSpan
import android.util.TypedValue
import android.view.View
import cn.android.basic.components.annoation.TextStyle

interface TextBuilder {

    /**
     * 设置文本颜色
     */
    fun color(color: Int)

    /**
     * 设置文本大小, 单位: sp
     */
    fun size(sp: Int) {
        TypedValue.COMPLEX_UNIT_PX
    }

    /**
     * 设置文本大小
     *
     * @param unit
     * [TypedValue.COMPLEX_UNIT_SP]
     * or [TypedValue.COMPLEX_UNIT_DIP]
     * or [TypedValue.COMPLEX_UNIT_PX]
     * @param size 文本大小
     */
    fun size(unit: Int, size: Int)

    /**
     * 添加下划线
     */
    fun underline()

    /**
     * 设置点击事件
     */
    fun click(listener: ClickableSpan?)

    /**
     * 设置点击事件
     */
    fun click(listener: (widget: View) -> Unit)

    /**
     * 设置文本样式: 粗体, 斜体, 普通
     * @param textStyle [Typeface.BOLD] or [Typeface.NORMAL] or [Typeface.ITALIC]
     * @see Typeface
     */
    fun style(@TextStyle textStyle: Int)

    /**
     * 设置删除线
     */
    fun strikethrough()
}

interface IEndSpanner : ISpanner {
    fun end(): SpannableStringBuilder
    override fun image(resId: Int): IEndSpanner
    override fun image(resId: Int, height: Int): IEndSpanner
    override fun imageByWidth(resId: Int, width: Int): IEndSpanner
    override fun append(text: CharSequence?, block: TextBuilder.() -> Unit): IEndSpanner
    override fun append(text: Float): IEndSpanner
    override fun append(text: Char): IEndSpanner
    override fun append(text: Int): IEndSpanner
    override fun append(text: CharSequence?): IEndSpanner
}

interface ISpanner {
    fun image(resId: Int): ISpanner
    fun image(resId: Int, height: Int): ISpanner
    fun imageByWidth(resId: Int, width: Int): ISpanner
    fun append(text: CharSequence?, block: TextBuilder.() -> Unit): ISpanner

    /**
     * 不做特殊处理, 直接拼接至富文本中
     */
    fun append(text: Float): ISpanner
    fun append(text: Char): ISpanner
    fun append(text: Int): ISpanner
    fun append(text: CharSequence?): ISpanner
}